/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tt.conn;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author PARDmx
 */
public class GuardaPublicacion extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        //ConexionDBM con = new ConexionDBM();
        ConexionDBP con = new ConexionDBP();
        String[] ids = request.getParameterValues("ids[]");
        String[] mensajes = request.getParameterValues("men[]");
        ArrayList<String> fields, val, res;
        PrintWriter out = response.getWriter();
        int np = 0;
        System.out.println(ids[1]);
        out.write(ids[1]);
        if (ids == null) {
            out.write("<p>Error al leer publicaciones</p>");
        } else {
            fields = new ArrayList<String>();
            fields.add("id_publicacion");
            fields.add("texto");
            //lids = new ArrayList<String>(Arrays.asList(ids));
            //lmensajes = new ArrayList<String>(Arrays.asList(mensajes));
            if (con.conectarDB() == 1) {
                out.write("<p>Error al conectar a la BD</p>");
            } else {
                for (int i = 0; i < ids.length; i++) {
                    val = new ArrayList<String>();
                    val.add(ids[i]);
                    val.add(mensajes[i]);

                    res = con.publicacionExiste(val.get(0));

                    if (res.isEmpty()) {
                        np = 1;
                        //if (con.insertDBP("publicacion_raw", fields, val) == 1) {
                        String r = con.insertDB("publicacion_raw", fields, val);
                        if (!r.equalsIgnoreCase("ok")) {    
                            out.write("<p>Error al insertar</p>");
                        } else {
                            out.write("<p>PG</p>");
                        }
                    }

                }
                if(np == 0){
                    out.write("No tienes publicaciones nuevas que analizar");
                }
                con.desconectarDB();
            }
        }

        /*
         try {
         out.println("<!DOCTYPE html>");
         out.println("<html>");
         out.println("<head>");
         out.println("<title>Servlet GuardaPublicacion</title>");            
         out.println("</head>");
         out.println("<body>");
         out.println("<h1>Servlet GuardaPublicacion at " + request.getContextPath() + "</h1>");
         out.println("</body>");
         out.println("</html>");
         } finally {            
         out.close();
         }
         */
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
